﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices.ComTypes;
using System.Threading.Tasks;
using System.Dynamic;

namespace NFinal.Common.Plugin
{
    /// <summary>
    /// 反射帮助类
    /// </summary>
    public class ReflectionHelper
    {
        /// <summary>
        /// 根据程序集上下文加载指定程序集中的类
        /// </summary>
        /// <param name="pluginLoadContext">程序集上下文</param>
        /// <param name="assemblyName">程序集名称</param>
        /// <param name="pluginClassName">类全名称</param>
        /// <returns></returns>
        public static Type GetType(PluginLoadContext pluginLoadContext,string assemblyName, string pluginClassName)
        {
            Type serviceType = null;
            var assembly = pluginLoadContext.LoadFromAssemblyName(new AssemblyName(assemblyName));
            if (assembly == null)
            {
                throw new ArgumentException("程序集名称不对");
            }
            serviceType = assembly.GetType(pluginClassName);
            if (serviceType == null)
            {
                throw new ArgumentException("类名不正确");
            }
            return serviceType;
        }
    
        /// <summary>
        /// 根据提供类型及参数进行初始化
        /// </summary>
        /// <typeparam name="TService">返回类型</typeparam>
        /// <typeparam name="T1">初始化参数1类型</typeparam>
        /// <param name="serviceType">初始化类型</param>
        /// <param name="t1">初始化参数1</param>
        /// <returns></returns>
        public static TService GetInstance<TService>(Type serviceType)
        {
            Type[] constructorTypes = new Type[0];
            var constructor = serviceType.GetConstructor(constructorTypes);
            if (constructor == null)
            {
                throw new Exceptions.ConstructorException(serviceType.Assembly.FullName, serviceType.FullName);
            }
            var instance = constructor.Invoke(new object[]{  });
            return (TService)instance;
        }

        /// <summary>
        /// 执行同步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <returns></returns>
        public static object ExecuteMethod<TService>(TService instance,string methodName)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance,new object[] {  });
            return result;
        }
        /// <summary>
        /// 执行异步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <returns></returns>
        public static Task ExecuteMethodAsync<TService>(TService instance, string methodName)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance, new object[] { });
            return (Task)result;
        }
        
        /// <summary>
        /// 根据提供类型及参数进行初始化
        /// </summary>
        /// <typeparam name="TService">返回类型</typeparam>
        /// <typeparam name="T1">初始化参数1类型</typeparam>
        /// <typeparam name="T2">初始化参数2类型</typeparam>
        /// <param name="serviceType">初始化类型</param>
        /// <param name="t1">初始化参数1</param>
        /// <param name="t2">初始化参数2</param>
        /// <returns></returns>
        public static TService GetInstance<TService, T1>(Type serviceType, T1 t1)
        {
            Type[] constructorTypes = new Type[1];
            constructorTypes[0] = typeof(T1);
            var constructor = serviceType.GetConstructor(constructorTypes);
            if (constructor == null)
            {
                throw new Exceptions.ConstructorException(serviceType.Assembly.FullName, serviceType.FullName);
            }
            var instance = constructor.Invoke(new object[]{  t1 });
            return (TService)instance;
        }

        /// <summary>
        /// 执行同步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <returns></returns>
        public static object ExecuteMethod<TService, T1>(TService instance,string methodName, T1 t1)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance,new object[] {  t1 });
            return result;
        }
        /// <summary>
        /// 执行异步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <returns></returns>
        public static Task ExecuteMethodAsync<TService, T1>(TService instance, string methodName, T1 t1)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance, new object[] { t1 });
            return (Task)result;
        }
        
        /// <summary>
        /// 根据提供类型及参数进行初始化
        /// </summary>
        /// <typeparam name="TService">返回类型</typeparam>
        /// <typeparam name="T1">初始化参数1类型</typeparam>
        /// <typeparam name="T2">初始化参数2类型</typeparam>
        /// <typeparam name="T3">初始化参数3类型</typeparam>
        /// <param name="serviceType">初始化类型</param>
        /// <param name="t1">初始化参数1</param>
        /// <param name="t2">初始化参数2</param>
        /// <param name="t3">初始化参数3</param>
        /// <returns></returns>
        public static TService GetInstance<TService, T1, T2>(Type serviceType, T1 t1, T2 t2)
        {
            Type[] constructorTypes = new Type[2];
            constructorTypes[0] = typeof(T1);
            constructorTypes[1] = typeof(T2);
            var constructor = serviceType.GetConstructor(constructorTypes);
            if (constructor == null)
            {
                throw new Exceptions.ConstructorException(serviceType.Assembly.FullName, serviceType.FullName);
            }
            var instance = constructor.Invoke(new object[]{  t1, t2 });
            return (TService)instance;
        }

        /// <summary>
        /// 执行同步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <returns></returns>
        public static object ExecuteMethod<TService, T1, T2>(TService instance,string methodName, T1 t1, T2 t2)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance,new object[] {  t1, t2 });
            return result;
        }
        /// <summary>
        /// 执行异步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <returns></returns>
        public static Task ExecuteMethodAsync<TService, T1, T2>(TService instance, string methodName, T1 t1, T2 t2)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance, new object[] { t1, t2 });
            return (Task)result;
        }
        
        /// <summary>
        /// 根据提供类型及参数进行初始化
        /// </summary>
        /// <typeparam name="TService">返回类型</typeparam>
        /// <typeparam name="T1">初始化参数1类型</typeparam>
        /// <typeparam name="T2">初始化参数2类型</typeparam>
        /// <typeparam name="T3">初始化参数3类型</typeparam>
        /// <typeparam name="T4">初始化参数4类型</typeparam>
        /// <param name="serviceType">初始化类型</param>
        /// <param name="t1">初始化参数1</param>
        /// <param name="t2">初始化参数2</param>
        /// <param name="t3">初始化参数3</param>
        /// <param name="t4">初始化参数4</param>
        /// <returns></returns>
        public static TService GetInstance<TService, T1, T2, T3>(Type serviceType, T1 t1, T2 t2, T3 t3)
        {
            Type[] constructorTypes = new Type[3];
            constructorTypes[0] = typeof(T1);
            constructorTypes[1] = typeof(T2);
            constructorTypes[2] = typeof(T3);
            var constructor = serviceType.GetConstructor(constructorTypes);
            if (constructor == null)
            {
                throw new Exceptions.ConstructorException(serviceType.Assembly.FullName, serviceType.FullName);
            }
            var instance = constructor.Invoke(new object[]{  t1, t2, t3 });
            return (TService)instance;
        }

        /// <summary>
        /// 执行同步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <returns></returns>
        public static object ExecuteMethod<TService, T1, T2, T3>(TService instance,string methodName, T1 t1, T2 t2, T3 t3)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance,new object[] {  t1, t2, t3 });
            return result;
        }
        /// <summary>
        /// 执行异步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <returns></returns>
        public static Task ExecuteMethodAsync<TService, T1, T2, T3>(TService instance, string methodName, T1 t1, T2 t2, T3 t3)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance, new object[] { t1, t2, t3 });
            return (Task)result;
        }
        
        /// <summary>
        /// 根据提供类型及参数进行初始化
        /// </summary>
        /// <typeparam name="TService">返回类型</typeparam>
        /// <typeparam name="T1">初始化参数1类型</typeparam>
        /// <typeparam name="T2">初始化参数2类型</typeparam>
        /// <typeparam name="T3">初始化参数3类型</typeparam>
        /// <typeparam name="T4">初始化参数4类型</typeparam>
        /// <typeparam name="T5">初始化参数5类型</typeparam>
        /// <param name="serviceType">初始化类型</param>
        /// <param name="t1">初始化参数1</param>
        /// <param name="t2">初始化参数2</param>
        /// <param name="t3">初始化参数3</param>
        /// <param name="t4">初始化参数4</param>
        /// <param name="t5">初始化参数5</param>
        /// <returns></returns>
        public static TService GetInstance<TService, T1, T2, T3, T4>(Type serviceType, T1 t1, T2 t2, T3 t3, T4 t4)
        {
            Type[] constructorTypes = new Type[4];
            constructorTypes[0] = typeof(T1);
            constructorTypes[1] = typeof(T2);
            constructorTypes[2] = typeof(T3);
            constructorTypes[3] = typeof(T4);
            var constructor = serviceType.GetConstructor(constructorTypes);
            if (constructor == null)
            {
                throw new Exceptions.ConstructorException(serviceType.Assembly.FullName, serviceType.FullName);
            }
            var instance = constructor.Invoke(new object[]{  t1, t2, t3, t4 });
            return (TService)instance;
        }

        /// <summary>
        /// 执行同步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <typeparam name="T4">参数4类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <returns></returns>
        public static object ExecuteMethod<TService, T1, T2, T3, T4>(TService instance,string methodName, T1 t1, T2 t2, T3 t3, T4 t4)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3), typeof(T4) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance,new object[] {  t1, t2, t3, t4 });
            return result;
        }
        /// <summary>
        /// 执行异步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <typeparam name="T4">参数4类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <returns></returns>
        public static Task ExecuteMethodAsync<TService, T1, T2, T3, T4>(TService instance, string methodName, T1 t1, T2 t2, T3 t3, T4 t4)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3), typeof(T4) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance, new object[] { t1, t2, t3, t4 });
            return (Task)result;
        }
        
        /// <summary>
        /// 根据提供类型及参数进行初始化
        /// </summary>
        /// <typeparam name="TService">返回类型</typeparam>
        /// <typeparam name="T1">初始化参数1类型</typeparam>
        /// <typeparam name="T2">初始化参数2类型</typeparam>
        /// <typeparam name="T3">初始化参数3类型</typeparam>
        /// <typeparam name="T4">初始化参数4类型</typeparam>
        /// <typeparam name="T5">初始化参数5类型</typeparam>
        /// <typeparam name="T6">初始化参数6类型</typeparam>
        /// <param name="serviceType">初始化类型</param>
        /// <param name="t1">初始化参数1</param>
        /// <param name="t2">初始化参数2</param>
        /// <param name="t3">初始化参数3</param>
        /// <param name="t4">初始化参数4</param>
        /// <param name="t5">初始化参数5</param>
        /// <param name="t6">初始化参数6</param>
        /// <returns></returns>
        public static TService GetInstance<TService, T1, T2, T3, T4, T5>(Type serviceType, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
        {
            Type[] constructorTypes = new Type[5];
            constructorTypes[0] = typeof(T1);
            constructorTypes[1] = typeof(T2);
            constructorTypes[2] = typeof(T3);
            constructorTypes[3] = typeof(T4);
            constructorTypes[4] = typeof(T5);
            var constructor = serviceType.GetConstructor(constructorTypes);
            if (constructor == null)
            {
                throw new Exceptions.ConstructorException(serviceType.Assembly.FullName, serviceType.FullName);
            }
            var instance = constructor.Invoke(new object[]{  t1, t2, t3, t4, t5 });
            return (TService)instance;
        }

        /// <summary>
        /// 执行同步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <typeparam name="T4">参数4类型</typeparam>
        /// <typeparam name="T5">参数5类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <param name="t5">参数5</param>
        /// <returns></returns>
        public static object ExecuteMethod<TService, T1, T2, T3, T4, T5>(TService instance,string methodName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance,new object[] {  t1, t2, t3, t4, t5 });
            return result;
        }
        /// <summary>
        /// 执行异步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <typeparam name="T4">参数4类型</typeparam>
        /// <typeparam name="T5">参数5类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <param name="t5">参数5</param>
        /// <returns></returns>
        public static Task ExecuteMethodAsync<TService, T1, T2, T3, T4, T5>(TService instance, string methodName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance, new object[] { t1, t2, t3, t4, t5 });
            return (Task)result;
        }
        
        /// <summary>
        /// 根据提供类型及参数进行初始化
        /// </summary>
        /// <typeparam name="TService">返回类型</typeparam>
        /// <typeparam name="T1">初始化参数1类型</typeparam>
        /// <typeparam name="T2">初始化参数2类型</typeparam>
        /// <typeparam name="T3">初始化参数3类型</typeparam>
        /// <typeparam name="T4">初始化参数4类型</typeparam>
        /// <typeparam name="T5">初始化参数5类型</typeparam>
        /// <typeparam name="T6">初始化参数6类型</typeparam>
        /// <typeparam name="T7">初始化参数7类型</typeparam>
        /// <param name="serviceType">初始化类型</param>
        /// <param name="t1">初始化参数1</param>
        /// <param name="t2">初始化参数2</param>
        /// <param name="t3">初始化参数3</param>
        /// <param name="t4">初始化参数4</param>
        /// <param name="t5">初始化参数5</param>
        /// <param name="t6">初始化参数6</param>
        /// <param name="t7">初始化参数7</param>
        /// <returns></returns>
        public static TService GetInstance<TService, T1, T2, T3, T4, T5, T6>(Type serviceType, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
        {
            Type[] constructorTypes = new Type[6];
            constructorTypes[0] = typeof(T1);
            constructorTypes[1] = typeof(T2);
            constructorTypes[2] = typeof(T3);
            constructorTypes[3] = typeof(T4);
            constructorTypes[4] = typeof(T5);
            constructorTypes[5] = typeof(T6);
            var constructor = serviceType.GetConstructor(constructorTypes);
            if (constructor == null)
            {
                throw new Exceptions.ConstructorException(serviceType.Assembly.FullName, serviceType.FullName);
            }
            var instance = constructor.Invoke(new object[]{  t1, t2, t3, t4, t5, t6 });
            return (TService)instance;
        }

        /// <summary>
        /// 执行同步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <typeparam name="T4">参数4类型</typeparam>
        /// <typeparam name="T5">参数5类型</typeparam>
        /// <typeparam name="T6">参数6类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <param name="t5">参数5</param>
        /// <param name="t6">参数6</param>
        /// <returns></returns>
        public static object ExecuteMethod<TService, T1, T2, T3, T4, T5, T6>(TService instance,string methodName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance,new object[] {  t1, t2, t3, t4, t5, t6 });
            return result;
        }
        /// <summary>
        /// 执行异步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <typeparam name="T4">参数4类型</typeparam>
        /// <typeparam name="T5">参数5类型</typeparam>
        /// <typeparam name="T6">参数6类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <param name="t5">参数5</param>
        /// <param name="t6">参数6</param>
        /// <returns></returns>
        public static Task ExecuteMethodAsync<TService, T1, T2, T3, T4, T5, T6>(TService instance, string methodName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance, new object[] { t1, t2, t3, t4, t5, t6 });
            return (Task)result;
        }
        
        /// <summary>
        /// 根据提供类型及参数进行初始化
        /// </summary>
        /// <typeparam name="TService">返回类型</typeparam>
        /// <typeparam name="T1">初始化参数1类型</typeparam>
        /// <typeparam name="T2">初始化参数2类型</typeparam>
        /// <typeparam name="T3">初始化参数3类型</typeparam>
        /// <typeparam name="T4">初始化参数4类型</typeparam>
        /// <typeparam name="T5">初始化参数5类型</typeparam>
        /// <typeparam name="T6">初始化参数6类型</typeparam>
        /// <typeparam name="T7">初始化参数7类型</typeparam>
        /// <typeparam name="T8">初始化参数8类型</typeparam>
        /// <param name="serviceType">初始化类型</param>
        /// <param name="t1">初始化参数1</param>
        /// <param name="t2">初始化参数2</param>
        /// <param name="t3">初始化参数3</param>
        /// <param name="t4">初始化参数4</param>
        /// <param name="t5">初始化参数5</param>
        /// <param name="t6">初始化参数6</param>
        /// <param name="t7">初始化参数7</param>
        /// <param name="t8">初始化参数8</param>
        /// <returns></returns>
        public static TService GetInstance<TService, T1, T2, T3, T4, T5, T6, T7>(Type serviceType, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
        {
            Type[] constructorTypes = new Type[7];
            constructorTypes[0] = typeof(T1);
            constructorTypes[1] = typeof(T2);
            constructorTypes[2] = typeof(T3);
            constructorTypes[3] = typeof(T4);
            constructorTypes[4] = typeof(T5);
            constructorTypes[5] = typeof(T6);
            constructorTypes[6] = typeof(T7);
            var constructor = serviceType.GetConstructor(constructorTypes);
            if (constructor == null)
            {
                throw new Exceptions.ConstructorException(serviceType.Assembly.FullName, serviceType.FullName);
            }
            var instance = constructor.Invoke(new object[]{  t1, t2, t3, t4, t5, t6, t7 });
            return (TService)instance;
        }

        /// <summary>
        /// 执行同步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <typeparam name="T4">参数4类型</typeparam>
        /// <typeparam name="T5">参数5类型</typeparam>
        /// <typeparam name="T6">参数6类型</typeparam>
        /// <typeparam name="T7">参数7类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <param name="t5">参数5</param>
        /// <param name="t6">参数6</param>
        /// <param name="t7">参数7</param>
        /// <returns></returns>
        public static object ExecuteMethod<TService, T1, T2, T3, T4, T5, T6, T7>(TService instance,string methodName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance,new object[] {  t1, t2, t3, t4, t5, t6, t7 });
            return result;
        }
        /// <summary>
        /// 执行异步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <typeparam name="T4">参数4类型</typeparam>
        /// <typeparam name="T5">参数5类型</typeparam>
        /// <typeparam name="T6">参数6类型</typeparam>
        /// <typeparam name="T7">参数7类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <param name="t5">参数5</param>
        /// <param name="t6">参数6</param>
        /// <param name="t7">参数7</param>
        /// <returns></returns>
        public static Task ExecuteMethodAsync<TService, T1, T2, T3, T4, T5, T6, T7>(TService instance, string methodName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance, new object[] { t1, t2, t3, t4, t5, t6, t7 });
            return (Task)result;
        }
        
        /// <summary>
        /// 根据提供类型及参数进行初始化
        /// </summary>
        /// <typeparam name="TService">返回类型</typeparam>
        /// <typeparam name="T1">初始化参数1类型</typeparam>
        /// <typeparam name="T2">初始化参数2类型</typeparam>
        /// <typeparam name="T3">初始化参数3类型</typeparam>
        /// <typeparam name="T4">初始化参数4类型</typeparam>
        /// <typeparam name="T5">初始化参数5类型</typeparam>
        /// <typeparam name="T6">初始化参数6类型</typeparam>
        /// <typeparam name="T7">初始化参数7类型</typeparam>
        /// <typeparam name="T8">初始化参数8类型</typeparam>
        /// <typeparam name="T9">初始化参数9类型</typeparam>
        /// <param name="serviceType">初始化类型</param>
        /// <param name="t1">初始化参数1</param>
        /// <param name="t2">初始化参数2</param>
        /// <param name="t3">初始化参数3</param>
        /// <param name="t4">初始化参数4</param>
        /// <param name="t5">初始化参数5</param>
        /// <param name="t6">初始化参数6</param>
        /// <param name="t7">初始化参数7</param>
        /// <param name="t8">初始化参数8</param>
        /// <param name="t9">初始化参数9</param>
        /// <returns></returns>
        public static TService GetInstance<TService, T1, T2, T3, T4, T5, T6, T7, T8>(Type serviceType, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8)
        {
            Type[] constructorTypes = new Type[8];
            constructorTypes[0] = typeof(T1);
            constructorTypes[1] = typeof(T2);
            constructorTypes[2] = typeof(T3);
            constructorTypes[3] = typeof(T4);
            constructorTypes[4] = typeof(T5);
            constructorTypes[5] = typeof(T6);
            constructorTypes[6] = typeof(T7);
            constructorTypes[7] = typeof(T8);
            var constructor = serviceType.GetConstructor(constructorTypes);
            if (constructor == null)
            {
                throw new Exceptions.ConstructorException(serviceType.Assembly.FullName, serviceType.FullName);
            }
            var instance = constructor.Invoke(new object[]{  t1, t2, t3, t4, t5, t6, t7, t8 });
            return (TService)instance;
        }

        /// <summary>
        /// 执行同步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <typeparam name="T4">参数4类型</typeparam>
        /// <typeparam name="T5">参数5类型</typeparam>
        /// <typeparam name="T6">参数6类型</typeparam>
        /// <typeparam name="T7">参数7类型</typeparam>
        /// <typeparam name="T8">参数8类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <param name="t5">参数5</param>
        /// <param name="t6">参数6</param>
        /// <param name="t7">参数7</param>
        /// <param name="t8">参数8</param>
        /// <returns></returns>
        public static object ExecuteMethod<TService, T1, T2, T3, T4, T5, T6, T7, T8>(TService instance,string methodName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance,new object[] {  t1, t2, t3, t4, t5, t6, t7, t8 });
            return result;
        }
        /// <summary>
        /// 执行异步函数
        /// </summary>
        /// <typeparam name="TService">对象类型</typeparam>
        /// <typeparam name="T1">参数1类型</typeparam>
        /// <typeparam name="T2">参数2类型</typeparam>
        /// <typeparam name="T3">参数3类型</typeparam>
        /// <typeparam name="T4">参数4类型</typeparam>
        /// <typeparam name="T5">参数5类型</typeparam>
        /// <typeparam name="T6">参数6类型</typeparam>
        /// <typeparam name="T7">参数7类型</typeparam>
        /// <typeparam name="T8">参数8类型</typeparam>
        /// <param name="instance">对象</param>
        /// <param name="methodName">方法名</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <param name="t5">参数5</param>
        /// <param name="t6">参数6</param>
        /// <param name="t7">参数7</param>
        /// <param name="t8">参数8</param>
        /// <returns></returns>
        public static Task ExecuteMethodAsync<TService, T1, T2, T3, T4, T5, T6, T7, T8>(TService instance, string methodName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8)
        {
            var serviceType = instance.GetType();
            var methodInfo = serviceType.GetMethod(methodName, new Type[] {  typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8) });
            if (methodInfo == null)
            {
                throw new Exceptions.MethodNotFoundException(serviceType.FullName,methodName);
            }
            var result = methodInfo.Invoke(instance, new object[] { t1, t2, t3, t4, t5, t6, t7, t8 });
            return (Task)result;
        }
        
    }
}
